<?php

namespace Admin\Frontend\Controllers;

use Complex\Exception;
use Helpers\Main;
use Helpers\UserPower;
use Helpers\Template;
use Helpers\Assets;
use Services\ConfigManage;

class OvertController extends ControllerBase
{
    /**
     * 短信模板列表页面
     *
     * create by ck 20161025
     * modify by fyh 20181210
     */
    public function msgTemplateListAction()
    {
        $this->view->pick('overt/msgTemplateList');
        Assets::easyUI('js/overt/msgTemplateList.js');
        Main::showNavPosition();
        UserPower::checkUserPower('Overt_MsgTemplate') or die(Main::showMessageModal('对不起，您没有查看此页面的权限！', 109112001));
        Template::exists('XHOvert_MsgTemplate', 'both') or die(Main::showMessageModal('配置文件不存在，请先创建！', 109112002));
        $listButtonArr = $formButtonArr = $listSearchArr = array();
        if (UserPower::checkUserPower('Overt_MsgTemplate_Add')) {
            $listButtonArr[] = array('id'=>'bt_list_add', 'text'=>'增加', 'plain'=>'true', 'icon'=>'fa-plus');
            $formButtonArr[] = array('id'=>'bt_form_add', 'text'=>'增加', 'class'=>'disabled');
            $this->view->setVar('AddEdit', true);
        }
        if (UserPower::checkUserPower('Overt_MsgTemplate_Edit')) {
            $formButtonArr[] = array('id'=>'bt_form_edit', 'text'=>'修改', 'class'=>'disabled');
            $this->view->setVar('AddEdit', true);
        }
        if (UserPower::checkUserPower('Overt_MsgTemplate_Del')) {
            $listButtonArr[] = array('id'=>'bt_list_del', 'text'=>'删除', 'plain'=>'true', 'icon'=>'fa-trash');
        }
        $formButtonArr[] = array('id'=>'bt_form_back', 'text'=>'返回');
        $formButtonArr[] = array('id'=>'bt_form_reset', 'text'=>'重置');
        $buttonToolbar = Template::createButtonToolBar($listButtonArr);
        $this->view->setVars(array(
            'List'          => Template::createList('List', 'XHOvert_MsgTemplate', [
                    'buttonToolbar' => $buttonToolbar
                ]),
            'Form'          => Template::createForm('XHOvert_MsgTemplate'),
            'FormButton'    => Template::createButtonToolBar($formButtonArr)
        ));
    }

    /**
     * 获取短信模板数据
     *
     * create by ck 20161025
     * modify by fyh 20181210
     */
    public function getMsgTemplateDataAction()
    {
        $this->view->disable();
        try {
            $result = ConfigManage::overtGetMsgTemplateData();
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
        } catch (Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

    /**
     * 新增/更改短信模板
     *
     * create by ck 20161025
     * modify by fyh 20181210
     */
    public function modifyMsgTemplateAction()
    {
        $this->view->disable();
        $this->request->isPost() or die;
        $realData = Main::getRealData($this->request->getPost());
        try {
            $result = ConfigManage::overtModifyMsgTemplate($realData);
            if ($result) {
                return $this->sendSuccessResponse('保存成功');
            }
        } catch (Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }

    /**
     * 批量删除短信模板
     *
     * create by ck 20161025
     * modify by fyh 20181210
     */
    public function delMsgTemplateAction()
    {
        $this->view->disable();
        $this->request->isPost() or die;
        UserPower::checkUserPower('Overt_MsgTemplate_Del') or die('（109112007）无操作权限！');
        $idArr = explode(',', $this->request->getPost('ids'));
        try {
            $result = ConfigManage::overtDelMsgTemplate($idArr);
            if ($result) {
                return '删除成功';
            }
        } catch (Exception $e) {
            return $this->sendErrorResponse(500, $e->getMessage());
        }
    }
}

